<?php
	chdir(dirname(__FILE__));
	ignore_user_abort(true); // 后台运行，不受前端断开连接影响
	set_time_limit(0); // 脚本将永久运行
	require_once ("../../config.php");//引入配置
	require_once ("../../log.php");
	require_once ("../api_tool.php");
	require_once ("core.php");
	require_once ("../api_wxsend_cat.php");
	// include ("../api_wxsend.php");

	// define("API_URL","http://127.0.0.1:8073/send");
	// define("API_URL","http://127.0.0.1:8073/send");
	// define("LOGIN_URL","http://127.0.0.1:1688/");
	// define("KEY","DB50FF2D30C14b68AB315D127BFDE9CF");

	/**
	 * @name 机器人拼多多发单Api接口
	 * @desc 本接口列表，只是用于处理当前机器人行为
	 */
	// LOGINFO($_GET, '=== _GET');

	$emoji_array = array (
		"[Commando]", "[Determined]", "[Clap]", "[Watermelon]", 	// 闲 努力 鼓掌 水果
		"[Beer]", "[Basketball]", "[PingPong]", "[Coffee]", 		// 酒 篮球 乒乓 咖啡
		"[Rice]", "[Rose]", "[Lips]", "[Heart]", "[Cake]",   		// 美食 鲜花 唇 爱心 生日
		"[强]","[玫瑰]","[太阳]","[耶]","[拳头]","[蛋糕]",
		"[嘴唇]","[爱心]","[跳跳]","[转圈]","[福]","[红包]",
	);

	$_wx = array();
	$_wx['bot_wxid'] = $_GET['robot_wxid'];
	$_wx['from_wxid'] = $_GET['wechat_room_id'];


	// 发单开头标语
	$_item = array();
	$_item['msg'] = BASE_PATH."boximg/".$_GET['good_img'];
	$_item['msg_type'] = 47;
	cat_send_msg($_wx, $_item);
	sleep(2);

	$rand_emoji = $emoji_array[array_rand($emoji_array)];
	// 组建标题文字说明
	$text = "{$rand_emoji}{$_GET['opt_name']}";
	$_item = array();
	$_item['msg'] = $text;
	$_item['msg_type'] = 1;
	cat_send_msg($_wx, $_item);
	sleep(2);

	// 发送连续小图
	$i = 0;
	while($_GET['goods_gallery_url'.$i]) {
		$imgpath = http_get_file($_GET['goods_gallery_url'.$i], BASE_PATH.'/img', "jpg");
		$_item = array();
		$_item['msg'] = $imgpath;
		$_item['msg_type'] = 3;
		cat_send_msg($_wx, $_item);
		sleep(2);
		++$i;
		if ($i >= 4) break;
	}

	$rand_emoji = $emoji_array[array_rand($emoji_array)];

	// 发送品牌名
	$_item = array();
	$_item['msg'] = "{$rand_emoji}{$_GET['goods_name']}";
	$_item['msg_type'] = 1;
	cat_send_msg($_wx, $_item);
	sleep(2);

	$min_group_price = ($_GET['min_group_price']?$_GET['min_group_price']:0)/100; 	// 原价
	$min_group_price = sprintf("%.2f", $min_group_price);
	$coupon_discount = ($_GET['coupon_discount']?$_GET['coupon_discount']:0)/100;	// 优惠券
	$coupon_discount = sprintf("%.2f", $coupon_discount);
	$co_final_price = sprintf("%.2f", $min_group_price-$coupon_discount);

	$resp = getPddDdkGoodsPromotionUrlGenerate($_GET['pdd_ance'], "[{$_GET['goods_id']}]");
	$Tpwd = $resp['goods_promotion_url_list'][0]['short_url'];

	$resp = getPddDdkGoodsPromotionUrlGenerate($_GET['pdd_ance'], "[{$_GET['goods_id']}]");
	$Tpwd = $resp['goods_promotion_url_list'][0]['short_url'];

	$rand_emoji = $emoji_array[array_rand($emoji_array)];
	$conpon_stage = "[@emoji=\uD83D\uDCE2]抢购链接\n".
		"—————————————\n".
		"原价：{$min_group_price}元\n".
		"优惠券：{$coupon_discount}元\n".
		"券后价：【{$co_final_price}】元{$rand_emoji} \n";

	$tbkTpwd = "[@emoji=\uD83D\uDC49]进入链接：{$Tpwd}【拼多多】";

	$item = array();
	$item['msg_type'] = 1;//1.回复消息 3.回复图片 43.回复视频 2004.回复文件 47.回复表情 49.回复链接
	$item['msg'] = $conpon_stage.$tbkTpwd;
	cat_send_msg($_wx, $item);
	$conpon_stage = str_replace('\\', '\\\\', $conpon_stage);
	$tbkTpwd = str_replace('\\', '\\\\', $tbkTpwd);
	// 拼多多的数据(用于后期发单列表的选择)
	simQuery("INSERT INTO item_send_catalog (shop_type, shop_name, price, concessional_price, final_price, wx_id, shop_code, clerk, pass_word) VALUES ".
		"(3, '{$_GET['opt_name']}', {$min_group_price}, {$coupon_discount}, {$co_final_price}, {$_GET['wx_id']}, {$_GET['goods_id']}, '{$conpon_stage}', '{$tbkTpwd}');");

	echo json_encode(array("result" => "成功"));
	exit();

?>